package com.clover.framework;

import java.util.HashMap;
import java.util.Map;

public class Page {
	private int showCount = 20; // 每页显示记录数
	private int totalPage; // 总页数
	private int totalResult; // 总记录数
	private int pageNo; // 当前页
	private int start; // 当前记录起始索引
	private String orderField; // 排序字段
	private String orderType = "asc"; // 正序或倒序
	private String showField=""; // 要显示的字段列表

	public String getShowField() {
		return showField;
	}

	public void setShowField(String showField) {
		this.showField = showField;
	}

	public String getOrderField() {
		return orderField;
	}

	public void setOrderField(String orderField) {
		this.orderField = orderField;
	}

	public String getOrderType() {
		return orderType;
	}

	public void setOrderType(String orderType) {
		this.orderType = orderType;
	}

	public Page() {
		this.showCount = 20;
	}

	public Page(int pageNo, int showCount) {
		this.pageNo = pageNo;
		this.showCount = showCount;
	}

	public int getTotalPage() {
		if (totalResult % showCount == 0)
			totalPage = totalResult / showCount;
		else
			totalPage = totalResult / showCount + 1;
		return totalPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public int getTotalResult() {
		return totalResult;
	}

	public void setTotalResult(int totalResult) {
		this.totalResult = totalResult;
	}

	public int getPageNo() {
		if (pageNo <= 0)
			pageNo = 1;
		/*if (pageNo > getTotalPage())
			pageNo = getTotalPage();*/
		return pageNo;
	}

	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}

	public int getShowCount() {
		return showCount;
	}

	public void setShowCount(int showCount) {

		this.showCount = showCount;
	}

	public int getStart() {
		start = (getPageNo() - 1) * getShowCount();
		if (start < 0)
			start = 0;
		return start;
	}

	public void setStart(int currentResult) {
		this.start = currentResult;
	}

	/**
	 * 设置分页参数
	 * @param page
	 * @param map
	 * @return
	 */
	public static Map setPageParams(Page page, Map map) {
		if (map == null) {
			map = new HashMap();
		}

		map.put("start", page.getStart());
		map.put("showCount", page.getShowCount());
		map.put("orderField", page.getOrderField());
		map.put("orderType", page.getOrderType());
		map.put("showField", page.getShowField());
		return map;
	}
}
